package com.cci.kangdao.controller;

import com.cci.kangdao.service.CampaignTaskInfoService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author linjiankun
 * @date 2023/10/23 15:46
 */
@Controller
public class CampaignTaskInfoController {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Autowired
    private CampaignTaskInfoService campaignTaskInfoService;


    /**
     * 通过工单编号获取sf系统campaign任务列表
     * @param
     * @return
     */
    @RequestMapping("/getCampaignTaskList.aspx")
    public ModelAndView getCampaignTaskList(HttpServletRequest request,
                                                HttpServletResponse response){

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return campaignTaskInfoService.getCampaignTaskList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }


    //站长保存campaign任务信息
    @RequestMapping(value = "/saveCampaignTask.aspx", method= RequestMethod.POST)
    public ModelAndView saveCampaignTask(@RequestBody String paramStr, HttpServletRequest request,
                                  HttpServletResponse response ) {
        try{
            return campaignTaskInfoService.saveCampaignTask(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }


    /**
     * 通过工单id获取campaign任务列表详情
     * @param
     * @return
     */
    @RequestMapping("/getCampaignTaskListByOrderId.aspx")
    public ModelAndView getCampaignTaskListByOrderId(HttpServletRequest request,
                                            HttpServletResponse response) throws Exception {

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return campaignTaskInfoService.getCampaignTaskListByOrderId(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }



    //技师主动接受campaign任务信息
    @RequestMapping(value = "/acceptCampaignTask.aspx", method= RequestMethod.POST)
    public ModelAndView acceptCampaignTask(@RequestBody String paramStr, HttpServletRequest request,
                                         HttpServletResponse response ) {
        try{
            return campaignTaskInfoService.acceptCampaignTask(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }



    //技师标记campaign任务状态
    @RequestMapping(value = "/tagCampaignTask.aspx", method= RequestMethod.POST)
    public ModelAndView tagCampaignTask(@RequestBody String paramStr, HttpServletRequest request,
                                           HttpServletResponse response ) {
        try{
            return campaignTaskInfoService.tagCampaignTask(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }





    //技师维修完成campaign任务并同步到crm
    @RequestMapping(value = "/repairFinishCampaignTask.aspx", method= RequestMethod.POST)
    public ModelAndView repairFinishCampaignTask(@RequestBody String paramStr, HttpServletRequest request,
                                           HttpServletResponse response ) {
        try{
            return campaignTaskInfoService.repairFinishCampaignTask(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }
}
